import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { PushService } from '../push.service';
import { FeathersService } from '../../api/feathers.service';
import Push from 'app/api/model/Push';
// tslint:disable-next-line:import-blacklist
import * as Rx from 'rxjs';
import { DialogComponent } from 'app/ui/compoment/dialog/dialog.component';
import { ChangeComponent } from 'app/ui/compoment/change/change.component';
import { NumPageComponent } from 'app/ui/num-page/num-page.component';

@Component({
  selector: 'app-push',
  templateUrl: './push.component.html',
  styleUrls: ['./push.component.css']
})
export class PushComponent implements OnInit {

  dataList: Rx.Observable<Push[]>
  checked: Push[] = []
  dataInput = new Push()

  num: number
  test: any
  @ViewChild('addDialog') addDialog: DialogComponent
  @ViewChild('changeDialog') changeDialog: ChangeComponent

  rows = []
  _pushservice: any
  oldtitle: any
  newdataInput = new Push()


  constructor(
    private pushService: PushService,
    // private pagenum: NumPageComponent
  ) {
    this._pushservice = pushService.service
    // this.test = this.pagenum.service
  }

  ngOnInit() {
    this.dataList = this.pushService.getDataList()

  }

  remove(push: Push) {
    this.pushService.removeData(push)
  }

  onClickAdd() {
    this.addDialog.show()
  }
  onClickchange(pp) {
    this.changeDialog.show()
    this.dataInput = pp
    this.oldtitle = this.dataInput.title;
    this.newdataInput = this.dataInput
  }
  dialogListener(event: string) {
    if (event === 'ok') {
      this.pushService.addData(this.dataInput)
        .then(() => this.dataInput = new Push())
        .catch(console.warn)
    }
    console.log('event:' + event)
  }

  changeListener(event: string) {
    if (event === 'ok') {
      this.pushService.changeData(this.newdataInput)
        .then(() => this.newdataInput = new Push())
        .catch(console.warn)
    }
  }

  isChecked(data: Push) {
    if (!data) {
      return false
    }
    return this.checked.filter(item => item.id === data.id).length > 0
  }

  checkAll(isChecked: boolean) {
    this.dataList.subscribe((result) => {
      isChecked ? this.checked = result.slice(0) : this.checked = []
    })
  }

  onCheckChanged(isChecked: boolean, data: Push) {
    if (this.checked.indexOf(data) >= 0) {
      this.checked = this.checked.filter(item => item.id !== data.id)
    }else {
      this.checked.push(data)
    }
    console.log('output:' + isChecked)
  }

}